<template>
  <el-container>
    <el-header>
      <h1 style="text-align: left; ">阳光学院学生管理系统</h1>
    </el-header>
    <el-container>
      <el-aside width="200px" class="left-menu" style="box-shadow: 2px 0 6px rgb(0 21 41 / 35%)">
        <el-menu default-active="/sys" class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff"
          active-text-color="#ffd04b" router>
          <el-menu-item index="/sys">个人信息</el-menu-item>
          <el-menu-item index="timetable">课表详情</el-menu-item>

        </el-menu>

      </el-aside>
      <el-main>

        <el-descriptions style="margin: 1% auto;" direction="vertical" title="个人信息：" :column="2" border>
          <template slot="extra">
            <el-button size="small" icon="el-icon-edit" type="primary" round @click="openEditDialog">修改</el-button>
          </template>

          <el-descriptions-item v-model="user.name" label="姓名">{{ user.name }}</el-descriptions-item>
          <el-descriptions-item v-model="user.sex" label="性别">{{ user.sex }}</el-descriptions-item>
          <el-descriptions-item v-model="user.studentNumber" label="学号">{{ user.studentNumber }}</el-descriptions-item>
          <el-descriptions-item v-model="user.enrollmentYear" label="入学年份"> {{ user.enrollmentYear }}
          </el-descriptions-item>
          <el-descriptions-item v-model="user.email" label="邮箱">{{ user.email }}</el-descriptions-item>
          <el-descriptions-item v-model="user.className" label="班级">
            {{ user.className }}
          </el-descriptions-item>
          <el-descriptions-item v-model="user.address" label="联系地址">{{ user.address }}</el-descriptions-item>

        </el-descriptions>


        <div style="margin-top:3% ;">
          <p><strong>考试成绩:</strong></p><el-empty style="margin:5% ;" description="暂无成绩"></el-empty>
        </div>

        <!-- 修改个人信息(只限邮箱和地址) -->
        <el-dialog title="修改个人信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
          <el-form :model="form" :rules="rules" ref="form" label-width="80px">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleSubmit">保存</el-button>
          </div>
        </el-dialog>

      </el-main>
    </el-container>
  </el-container>
</template>

<script>


export default {
  data() {
    return {
      // 个人信息
      user: {},
      dialogVisible: false,
      id:null,
      form: {
        id:null ,
        email: null,
        address: null,
      },
      rules: {
        email: [
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        address: [
          { min: 5, message: '太敷衍了吧老铁 , 写详细点呗~', trigger: "blur" }
        ]
      }

    }
  },
  mounted() {    //使用登录接口传递回的对象赋值给user对象渲染页面
    this.user = JSON.parse(sessionStorage.getItem('user'));
    console.log(this.user);
    this.form.id = this.user.id
    console.log(this.form);
  },
  methods: {
    load(){
      this.$axios.get("http://localhost:1010/user?id="+this.user.id).then((res =>{
        this.user = res.data
        sessionStorage.setItem('user', JSON.stringify(this.user));
      }))
    },
    
    //打开编辑窗口
    openEditDialog() {
      this.dialogVisible = true
      
    },
    // 关闭编辑窗口
    handleClose(done) {
      this.$refs.form.resetFields()
      done()
    },

    handleSubmit() {
      
      if (!this.form.email && !this.form.address) {
        this.$message.error('没改信息你提交啥？');
      } else {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$confirm('确定修改？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }).then(() => {
              console.log(this.form);
              // if(this.form.address == null){
              //   this.form.address = '暂无'
              // } else if(this.form.email == null){
              //       this.form.email = '暂无'
              // }
              //此处为向后端接口发送修改请求
              this.$axios.patch("http://localhost:1010/user/update",
                               {...this.form}            
              ).then((res => {
                console.log("修改成功");
                console.log(res);
                this.dialogVisible = false;
              this.$refs.form.resetFields();
              this.$message({
                type: 'success',
                message: '修改成功!',
              });
              this.load()
              })).catch((error) => {
                console.log("修改失败");
                console.log(error)
                this.$message.error('服务器异常');
              })
            })
          }
        })
      }
    }
  }
}

</script>


<style>
.left-menu {
  height: 100vh;
}

.el-menu-vertical-demo {
  height: 100vh;
}

.el-header {
  background-color: #B3C0D1;
  color: #333;
  height: 50%;
}
</style>
